package com.ktjy.ruoyimes.pro.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktjy.ruoyimes.pro.mapper.ProWorkorderMapper;
import com.ktjy.ruoyimes.pro.domain.ProWorkorder;
import com.ktjy.ruoyimes.pro.service.IProWorkorderService;

/**
 * 生产工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class ProWorkorderServiceImpl implements IProWorkorderService 
{
    @Autowired
    private ProWorkorderMapper proWorkorderMapper;

    /**
     * 查询生产工单
     * 
     * @param workorderId 生产工单主键
     * @return 生产工单
     */
    @Override
    public ProWorkorder selectProWorkorderByWorkorderId(Long workorderId)
    {
        return proWorkorderMapper.selectProWorkorderByWorkorderId(workorderId);
    }

    /**
     * 查询生产工单列表
     * 
     * @param proWorkorder 生产工单
     * @return 生产工单
     */
    @Override
    public List<ProWorkorder> selectProWorkorderList(ProWorkorder proWorkorder)
    {
        return proWorkorderMapper.selectProWorkorderList(proWorkorder);
    }

    /**
     * 新增生产工单
     * 
     * @param proWorkorder 生产工单
     * @return 结果
     */
    @Override
    public int insertProWorkorder(ProWorkorder proWorkorder)

    {
        // 设置默认订单来源
        if (proWorkorder.getOrderSource() == null || proWorkorder.getOrderSource().isEmpty()) {
            proWorkorder.setOrderSource("MANUAL");
        }

        // 确保productId有值
        if (proWorkorder.getProductId() == null) {
            throw new IllegalArgumentException("productId不能为空");
        }

        // 设置ancestors（根据业务逻辑）
        if (proWorkorder.getAncestors() == null) {
            // 示例：设置为空字符串或从其他地方获取
            proWorkorder.setAncestors("0"); // 或其他默认值
            // 或者根据你的业务逻辑设置更复杂的值
        }

        proWorkorder.setCreateTime(DateUtils.getNowDate());
        return proWorkorderMapper.insertProWorkorder(proWorkorder);
    }

    /**
     * 修改生产工单
     * 
     * @param proWorkorder 生产工单
     * @return 结果
     */
    @Override
    public int updateProWorkorder(ProWorkorder proWorkorder)
    {
        proWorkorder.setUpdateTime(DateUtils.getNowDate());
        return proWorkorderMapper.updateProWorkorder(proWorkorder);
    }

    /**
     * 批量删除生产工单
     * 
     * @param workorderIds 需要删除的生产工单主键
     * @return 结果
     */
    @Override
    public int deleteProWorkorderByWorkorderIds(Long[] workorderIds)
    {
        return proWorkorderMapper.deleteProWorkorderByWorkorderIds(workorderIds);
    }

    /**
     * 删除生产工单信息
     * 
     * @param workorderId 生产工单主键
     * @return 结果
     */
    @Override
    public int deleteProWorkorderByWorkorderId(Long workorderId)
    {
        return proWorkorderMapper.deleteProWorkorderByWorkorderId(workorderId);
    }
}
